package xcu.lxj.nettychat.common.domain.vo.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;
import xcu.lxj.nettychat.common.domain.enums.ResCode;

import java.io.Serial;
import java.io.Serializable;

/**
 * @Author XinJian Lei
 * @Date 2024/9/29 11:47
 * @Version 1.0
 */

@Data
@NoArgsConstructor
public class ApiResult<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    @Schema(description = "响应码")
    private int code;

    @Schema(description = "回执消息描述")
    private String msg;

    private T data;

    public static <T> ApiResult<T> success() {
        return restResult(null, ResCode.SUCCESS.getCode(), ResCode.SUCCESS.getMessage());
    }

    public static <T> ApiResult<T> success(T data) {
        return restResult(data, ResCode.SUCCESS.getCode(), ResCode.SUCCESS.getMessage());
    }


    public static <T> ApiResult<T> success(String msg, T data) {
        return restResult(data, ResCode.SUCCESS.getCode(), msg);
    }

    public static <T> ApiResult<T> fail(ResCode resCode) {
        return restResult(null, resCode.getCode(), resCode.getMessage());
    }

    public static <T> ApiResult<T> fail() {
        return restResult(null, ResCode.INTERNAL_SERVER_ERROR.getCode(), "操作失败");
    }

    public static <T> ApiResult<T> fail(T data) {
        return restResult(data, ResCode.INTERNAL_SERVER_ERROR.getCode(), "操作失败");
    }

    public static <T> ApiResult<T> fail(String msg, T data) {
        return restResult(data, ResCode.INTERNAL_SERVER_ERROR.getCode(), msg);
    }

    public static <T> ApiResult<T> fail(int code, String msg) {
        return restResult(null, code, msg);
    }

    private static <T> ApiResult<T> restResult(T data, int code, String msg) {
        ApiResult<T> r = new ApiResult<>();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }
}